﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Agent_Default : AgentPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindData();
    }
    protected void BindData()
    {
        MatchIns.Model.TAgent aAgent = new MatchIns.BLL.TAgent().GetModel(UserInfo.UserID);
        lUserName.Text = UserInfo.UserName;
        lEmail.Text = UserInfo.Email;
        if (aAgent == null) return;
        tbName.Text = aAgent.Name.Trim();
        if (aAgent.Sex == "男")
            rblSex.SelectedValue = "男";
        if (aAgent.Sex == "女")
            rblSex.SelectedValue = "女";
        tbIDCard.Text = aAgent.IDCard.Trim();
        if (aAgent.Birthday != DateTime.MinValue)
            tbBirth.Text = aAgent.Birthday.ToString("yyyy-MM-dd");
        tbCompany.Text = aAgent.Company.Trim();
        tbPost.Text = aAgent.Post.Trim();
        tbWeb.Text = aAgent.PersonWeb.Trim();
        tbAddress.Text = aAgent.Address.Trim();
        tbZipCode.Text = aAgent.ZipCode.Trim();
        tbTel.Text = aAgent.Tel.Trim();
        tbFax.Text = aAgent.Fax.Trim();
        tbMobile.Text = aAgent.MobilePhone.Trim();
        tbMSN.Text = aAgent.Msn.Trim();
        tbQQ.Text = aAgent.QQ.Trim();
        tbAliWW.Text = aAgent.AliWW.Trim();
        hfProvince.Value = aAgent.ProvinceID.ToString();
        hfCity.Value = aAgent.CityID.ToString();
        district.Value = aAgent.DistrictID.ToString();
        if (aAgent.Phone == "")
        {
            iPhoto.ImageUrl = "/images/grxx/pic.gif";
            hfPhoto.Value = "";
        }
        else
        {
            iPhoto.ImageUrl = "~/UploadFile/Photo/Thumb/" + aAgent.Phone.Trim(); ;
            hfPhoto.Value = aAgent.Phone.Trim(); ;
        }

    }
    protected void save_Click(object sender, ImageClickEventArgs e)
    {
        MatchIns.Model.TAgent aAgent = new MatchIns.Model.TAgent();
        aAgent.UserID = UserInfo.UserID;
        aAgent.Name = tbName.Text;
        aAgent.Sex = rblSex.SelectedValue;
        aAgent.IDCard = tbIDCard.Text;
        DateTime birth;
        if (DateTime.TryParse(tbBirth.Text, out birth))
            aAgent.Birthday = birth;
        else
            aAgent.Birthday = DateTime.MinValue;
        aAgent.Company = tbCompany.Text;
        aAgent.Post = tbPost.Text;
        aAgent.PersonWeb = tbWeb.Text;
        aAgent.Address = tbAddress.Text;
        aAgent.ZipCode = tbZipCode.Text;
        aAgent.Tel = tbTel.Text;
        aAgent.Fax = tbFax.Text;
        aAgent.MobilePhone = tbMobile.Text;
        aAgent.Msn = tbMSN.Text;
        aAgent.QQ = tbQQ.Text;
        aAgent.AliWW = tbAliWW.Text;
        aAgent.ProvinceID = Area.ProviceId;
        aAgent.CityID = Area.CityId;
        aAgent.DistrictID = Area.DistrictId;
        if (uploadFile.HasFile)
        {
            hfPhoto.Value = MatchIns.Common.Upload.UploadImage(uploadFile, "~/UploadFile/Photo/", 90);
            iPhoto.ImageUrl = "~/UploadFile/Photo/Thumb/" + hfPhoto.Value;
        }
        aAgent.Phone = hfPhoto.Value;

        if (new MatchIns.BLL.TAgent().Update(aAgent))
            errors.Text = "<script type='text/javascript'>alert('√信息保存成功');location.href='default.aspx';</script>";
        else
            errors.Text = "信息保存失败";
        //Response.Redirect("UserInfo.aspx");
    }
}
